
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">

<ui:composition template="/template/template.xhtml">

	<ui:define name="content">
		<h:form id="listaNotaFiscal">
			<br />
			<p:commandButton actionListener="#{notaFiscalMB.novo}" value="Nova"
				update=":editaNotaFiscal" oncomplete="salvaNotaFiscal.show()" />
			<br />
			<br />
			<p:dataTable value="#{notaFiscalMB.listaNotasFiscais}"
				var="notaFiscal">

				<p:column headerText="ID"> #{notaFiscal.id}</p:column>
				<p:column headerText="Cliente"> #{notaFiscal.cliente}</p:column>
				<p:column headerText="Data Compra">
					<h:outputText value="#{notaFiscal.dataCompra}">
						<f:convertDateTime pattern="dd/MM/yyyy" />
					</h:outputText>
				</p:column>
				<p:column headerText="Valor Total">
					<h:outputText value="#{notaFiscal.valorTotal}">
						<f:convertNumber maxFractionDigits="2" minFractionDigits="2"
							type="currency" />
					</h:outputText>
				</p:column>
				<p:column>
					<p:commandButton value="Excluir"
						actionListener="#{notaFiscalMB.excluir}"
						update=":listaNotaFiscal">
						<f:setPropertyActionListener target="#{notaFiscalMB.notaFiscal}"
							value="#{notaFiscal}"></f:setPropertyActionListener>
					</p:commandButton>
					<p:commandButton value="Editar" update=":editaNotaFiscal" oncomplete="salvaNotaFiscal.show()">
						<f:setPropertyActionListener target="#{notaFiscalMB.notaFiscal}"
							value="#{notaFiscal}"></f:setPropertyActionListener>
					</p:commandButton>
				</p:column>
			</p:dataTable>
			<h:messages></h:messages>
		</h:form>

		<p:dialog widgetVar="salvaNotaFiscal" modal="true" width="550px">
			<h:form id="editaNotaFiscal">
				<h:panelGrid id="notaCliente" columns="2">
					<h:outputText value="Cliente" />
					<p:selectOneMenu id="cliente" requiredMessage="Cliente obrigatório"
						value="#{notaFiscalMB.notaFiscal.cliente}"
						converter="clienteConverter">
						<f:selectItems value="#{notaFiscalMB.clientes}" var="cliente"
							itemValue="#{cliente.id}" itemLabel="#{cliente.nome}"
							noSelectionValue="Selecione um cliente" />
					</p:selectOneMenu>

					<h:outputText value="Data Compra" />
					<p:calendar id="dataCompra"
						value="#{notaFiscalMB.notaFiscal.dataCompra}"
						requiredMessage="Data Obrigatória" />
				</h:panelGrid>
				<h:outputText value="Produtos" />

				<p:dataTable id="produtos"
					selection="#{notaFiscalMB.selectedProduto}"
					value="#{notaFiscalMB.produtoDataModel}" selectionMode="single"
					var="produto">

					<p:ajax event="rowSelect" update=":editaNotaFiscal:detalhesProduto"
						listener="#{notaFiscalMB.onRowSelect}" />

					<p:column headerText="Nome">
							#{produto.nome}
						</p:column>

					<p:column headerText="Valor">

						<h:outputText value="#{produto.valor}">
							<f:convertNumber maxFractionDigits="2" minFractionDigits="2"
								type="currency" />
						</h:outputText>
					</p:column>
				</p:dataTable>
				<br />
				<h:panelGrid id="detalhesProduto" columns="4">
					<h:outputText value="Quantidade" />
					<p:inputMask mask="?99" size="2"
						value="#{notaFiscalMB.itemNotaFiscal.quantidade}" id="quantidade">
						<f:ajax event="blur" execute="@this"
							listener="#{notaFiscalMB.atualizaTotalProduto}"
							render=":editaNotaFiscal:detalhesProduto" />
					</p:inputMask>
					<h:outputText value="Valor" />
					<h:outputText value="#{notaFiscalMB.itemNotaFiscal.valor}">
						<f:convertNumber maxFractionDigits="2" minFractionDigits="2"
							type="currency" />
					</h:outputText>

					<p:commandButton value="Adicionar" process="detalhesProduto"
						actionListener="#{notaFiscalMB.adicionarProduto}"
						update=":editaNotaFiscal:detalhesNota" />

				</h:panelGrid>

				<br />

				<h:panelGrid id="detalhesNota" columns="2">
					<h:outputText value="Produtos" />
					<p:dataTable id="produtosNota"
						value="#{notaFiscalMB.notaFiscal.itensNotaFiscal}" var="item">

						<p:column headerText="Nome">
							#{item.produto.nome}
						</p:column>

						<p:column headerText="Valor">
							#{item.produto.valor}
						</p:column>
						<p:column headerText="Valor">
							#{item.quantidade}
						</p:column>
						<p:column headerText="Valor">
							<h:outputText value="#{item.valor}">
								<f:convertNumber maxFractionDigits="2" minFractionDigits="2"
									type="currency" />
							</h:outputText>
						</p:column>
					</p:dataTable>

					<h:outputText value="Total Nota" />
					<h:outputText value="#{notaFiscalMB.notaFiscal.valorTotal}" />

					<p:commandButton actionListener="#{notaFiscalMB.salvar}"
						oncomplete="salvaNotaFiscal.hide()" value="Salvar"
						update=":listaNotaFiscal" process="@form" />
				</h:panelGrid>


			</h:form>
		</p:dialog>


	</ui:define>

</ui:composition>
</html>